package com.dailyyoga.bitmapManager;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.net.tool.DownLoadTask;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class RoundImageLoader extends YogaImageLoader {

	public RoundImageLoader(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	RecyclingBitmapDrawable getBitmapFromFile(String url) {
		Bitmap bitmap = null;
		String fileName = getMD5Str(url);
		if (fileName == null) {
			return null;
		}
		String filePath = DownLoadTask.path + fileName;
		try {
			FileInputStream fis = new FileInputStream(filePath);
			bitmap = BitmapFactory.decodeStream(fis);
			System.out.println("在本地缓存中找到图片===" + filePath);

			bitmap = toRoundCorner(bitmap);

			RecyclingBitmapDrawable bitmapDrawable = new RecyclingBitmapDrawable(
					mContext.getResources(), bitmap);
			bitmapDrawable.setIsCached(true);

			return bitmapDrawable;
		} catch (FileNotFoundException e) {
			System.out.println("getBitmapFromFile===" + e.toString());
			e.printStackTrace();
			return null;
		}
	}

	private Bitmap toRoundCorner(Bitmap bitmap) {
		int w = 400;

		Bitmap output = Bitmap.createBitmap(w, w, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = Color.parseColor("#000000");

		int x = bitmap.getWidth() > bitmap.getHeight() ? (bitmap.getWidth() - bitmap
				.getHeight()) / 2 : 0;

		int y = bitmap.getHeight() > bitmap.getWidth() ? (bitmap.getHeight() - bitmap
				.getWidth()) / 2 : 0;

		int sw = bitmap.getWidth() < bitmap.getHeight() ? bitmap.getWidth()
				: bitmap.getHeight();

		if (sw > 400) {
			sw = 400;
		}

		float k = w * 1f / sw;

		int dw = (int) (bitmap.getWidth() * k);
		int dh = (int) (bitmap.getHeight() * k);

		final Rect src = new Rect(x, y, bitmap.getWidth() - 2 * x,
				bitmap.getHeight() - 2 * y);

		final Rect dst = new Rect(0, 0, dw, dh);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		canvas.drawARGB(0, 0xff, 0xff, 0xff);
		paint.setColor(color);
		canvas.drawCircle(w / 2, w / 2, w / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		drawCircleBorder(canvas, w / 2, w);
		canvas.save();
		bitmap.recycle();

		return output;
	}

	private void drawCircleBorder(Canvas canvas, int radius, int w) {
		Paint paint = new Paint();
		/* 去锯齿 */
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);

		/* 设置paint的　style　为STROKE：空心 */
		paint.setStyle(Paint.Style.STROKE);
		/* 设置paint的外框宽度 */
		paint.setStrokeWidth(dp2px(2));
		paint.setColor(0x1800000);
		canvas.drawCircle(w / 2, w / 2, radius - dp2px(2), paint);
		paint.setStrokeWidth(dp2px(4));
		paint.setColor(0xffffffff);
		canvas.drawCircle(w / 2, w / 2, radius - dp2px(2), paint);

	}

	private float dp2px(int dp) {

		return mContext.getResources().getDisplayMetrics().density * dp;

	}

}
